<?php

namespace app\api\controller;

use app\ApiBaseController;
use app\admin\model\{Orders, Records};

class Order extends ApiBaseController
{
    /**
     * 按类型批量获取订单  0进行中    1已完成    2已结算
     */
    public function getOrdersList()
    {
        $state = $this->request->param('state/d', 0);
        if (!in_array($state, [0,1,2])){
            return result(1,'订单类型不存在');
        }
        $data = Orders::where(['uid'=>$this->uid, 'state'=>$state])->select();
        if ($data->isEmpty()){
            return result(0, '', []);
        }
        foreach ($data as $k => $v){
            $data[$k]['true_name'] = $this->user->true_name;
            if ($v['state'] <> 2){
                $data[$k] = $v->hidden(['ratio_amount', 'ratio_time']);
            }
        }
        return result(0, '', $data);
    }

    /**
     * 获取单个订单
     */
    public function getOrder()
    {
        $id = $this->request->param('id/d');
        $order = Orders::find($id);
        if (empty($order)){
            return result(1,'订单不存在');
        }
        if ($order->uid <> $this->uid){
            return result(1,'只能操作自己的订单');
        }
        $order['true_name'] = $this->user->true_name;
        if ($order['state'] <> 2){
            $order = $order->hidden(['ratio_amount', 'ratio_time']);
        }
        return result(0, '', $order);
    }

    /**
     *  修改订单，等待审核
     */
    public function updateOrder()
    {
        $post = $this->request->post();
        $rule = [
            'order_id|订单编号'         =>  'require|number',
            'state|订单状态'            =>  'in:0,1',
            'add_pay_amount|新增金额'   =>  'float',
        ];
        $this->apiValidate($post, $rule);
        $order = Orders::find($post['order_id']);
        if (empty($order)){
            return result(1,'订单不存在');
        }
        if ($order->uid <> $this->uid){
            return result(1,'只能操作自己的订单');
        }
        if ($order->state == 1){
            return result(1,'订单已完成，无法修改');
        }
        if ($order->state == 2){
            return result(1,'订单已结算，无法修改');
        }
        if (($order->state == $post['state']) && $post['add_pay_amount'] == 0){
            return result(1,'没有改动，提交失败');
        }
        try{
            Records::create([
                'audit_type'    =>  0,
                'order_id'      =>  $order->id,
                'uid'           =>  $order->uid,
                'name'          =>  $order->name,
                'type'          =>  $order->type,
                'demand'        =>  $order->demand,
                'phone'         =>  $order->phone,
                'qq'            =>  $order->qq,
                'weixin'        =>  $order->weixin,
                'state'         =>  $order->state,
                'total_amount'  =>  $order->total_amount,
                'pay_amount'    =>  $order->pay_amount,
                'ratio'         =>  $order->ratio,
                'add_pay_amount'=>  $post['add_pay_amount'],
                'new_state'     =>  $post['state'],
            ]);
        }catch (\Exception $e){
            return result(1, $e->getMessage());
        }
        return result(0,'提交成功');
    }

    /**
     * 创建订单，等待审核
     */
    public function createOrder()
    {
        $post = $this->request->post();
        $rule = [
            'name|客户姓名'             =>  'chsAlphaNum',
			'phone|手机号'				=>	'mobile',
            'type|类型'                 =>  'require|in:0,1',
            'total_amount|成交金额'     =>  'float',
            'pay_amount|已收金额'       =>  'float',
            'ratio|提成比例'            =>  'float',
        ];
        $this->apiValidate($post, $rule);
        if (empty($post['qq']) && empty($post['weixin']) && empty($post['phone'])){
            return result(1,'联系方式至少填一个');
        }
        try{
            Records::create([
                'audit_type'    =>  1,
                'uid'           =>  $this->uid,
                'name'          =>  $post['name'] ?? '',
                'type'          =>  $post['type'],
                'demand'        =>  $post['demand'] ?? '',
                'phone'         =>  $post['phone'] ?? '',
                'qq'            =>  $post['qq'] ?? '',
                'weixin'        =>  $post['weixin'] ?? '',
                'total_amount'  =>  $post['total_amount'] ?? 0,
                'pay_amount'    =>  $post['pay_amount'] ?? 0,
                'ratio'         =>  $post['ratio'] ?? 0,
            ]);
        }catch (\Exception $e){
            return result(1, $e->getMessage());
        }
        return result(0,'提交成功');
    }
}